package cn.signalpha.modules.shop.service;

import cn.signalpha.modules.shop.entity.CouponUser;
import cn.signalpha.modules.shop.mapper.CouponUserMapper;
import cn.signalpha.modules.shop.vo.CouponUserVO;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;

/**
 * 服务类
 *
 * @author signalpha
 */
@Service
public class CouponUserService extends ServiceImpl<CouponUserMapper, CouponUser> {

	/**
	 * 自定义分页
	 */
    public IPage<CouponUserVO> selectCouponUserPage(IPage<CouponUserVO> page, Map<String, Object> params) {
        return page.setRecords(baseMapper.selectCouponUserPage(page, params));
    }

	/**
	 * 获取用户所有优惠券
	 */
	public List<CouponUser> listByUserId(Long userId) {
		return baseMapper.selectList(Wrappers.<CouponUser>lambdaQuery().eq(CouponUser::getUserId, userId).eq(CouponUser::getStatus, 0));
	}

	/**
	 * 获取用户再某个商户的所有优惠券
	 */
	public List<CouponUser> listByUserIdAndSellerId(Long userId, Long sellerId) {
		return baseMapper.selectList(Wrappers.<CouponUser>lambdaQuery().eq(CouponUser::getUserId, userId).eq(CouponUser::getSellerId, sellerId).eq(CouponUser::getStatus, 0));
	}

	/**
	 * 用户是否领取了优惠券
	 */
	public int isGet(Long couponId, Long userId) {
		long count = baseMapper.selectCount(Wrappers.<CouponUser>lambdaQuery()
			.eq(CouponUser::getCouponId, couponId).eq(CouponUser::getUserId, userId).eq(CouponUser::getStatus, 0)
		);
		return count > 0 ? 1 : 0;
	}

}

